#ifndef __BUFFER_H__
#define __BUFFER_H__

#include <vector>
#include <atomic>
#include <cstring>
#include <string>
#include <assert.h>
#include <sys/uio.h>
#include <unistd.h>

class Buffer {
public:
    static const int MAX_SOCKT_BUFFER_SIZE = 65536;

    Buffer(int initBufferSize = 1024);
    ~Buffer() = default;

    size_t ReadableBytes() const;
    size_t WriteableBytes() const;
    size_t PrependableBytes() const;

    char* BeginPtr();
    const char* BeginPtr() const;

    char* BeginRead();
    const char* BeginReadConst() const;
    char* BeginWrite();
    const char* BeginWrite() const;

    void Retrieve(int len);
    void RetrieveUntil(const char* end);
    void RetrieveAll();
    void HasWrite(size_t len);

    void EnsureWriteable(size_t len);
    void MakeSpace(size_t len);

    std::string RetrieveAllToString();

    void Append(const char* str, size_t len);
    void Append(const std::string &str);
    void Append(const void* data, size_t len);
    void Append(const Buffer& buffer);

    ssize_t ReadFd(int fd, int* Errno);
    ssize_t WriteFd(int fd, int* Errno);


private:
    std::vector<char> buffer_;
    std::atomic<size_t> readPos_;
    std::atomic<size_t> writePos_;
};

#endif